// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_program/fragment_five/StaticList.dart';
import 'package:intl/intl.dart';
import 'package:shimmer/shimmer.dart';

class _InputDropdown extends StatelessWidget {
  const _InputDropdown(
      {Key key,
      this.child,
      this.labelText,
      this.valueText,
      this.valueStyle,
      this.onPressed})
      : super(key: key);

  final String labelText;
  final String valueText;
  final TextStyle valueStyle;
  final VoidCallback onPressed;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return new InkWell(
      onTap: onPressed,
      child: new InputDecorator(
        decoration: new InputDecoration(
          labelText: labelText,
        ),
        baseStyle: valueStyle,
        child: new Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            new Text(valueText, style: valueStyle),
            new Icon(Icons.arrow_drop_down,
                color: Theme.of(context).brightness == Brightness.light
                    ? Colors.grey.shade700
                    : Colors.white70),
          ],
        ),
      ),
    );
  }
}

class _DateTimePicker extends StatelessWidget {
  const _DateTimePicker(
      {Key key,
      this.labelText,
      this.selectedDate,
      this.selectedTime,
      this.selectDate,
      this.selectTime})
      : super(key: key);

  final String labelText;
  final DateTime selectedDate;
  final TimeOfDay selectedTime;
  final ValueChanged<DateTime> selectDate;
  final ValueChanged<TimeOfDay> selectTime;

  Future<Null> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: new DateTime(2015, 8),
        lastDate: new DateTime(2101));
    if (picked != null && picked != selectedDate) selectDate(picked);
  }

  Future<Null> _selectTime(BuildContext context) async {
    final TimeOfDay picked =
        await showTimePicker(context: context, initialTime: selectedTime);
    if (picked != null && picked != selectedTime) selectTime(picked);
  }

  @override
  Widget build(BuildContext context) {
    final TextStyle valueStyle = Theme.of(context).textTheme.title;
    return new Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: <Widget>[
        new Expanded(
          flex: 4,
          child: new _InputDropdown(
            labelText: labelText,
            valueText: new DateFormat.yMMMd().format(selectedDate),
            valueStyle: valueStyle,
            onPressed: () {
              _selectDate(context);
            },
          ),
        ),
        const SizedBox(width: 12.0),
        new Expanded(
          flex: 3,
          child: new _InputDropdown(
            valueText: selectedTime.format(context),
            valueStyle: valueStyle,
            onPressed: () {
              _selectTime(context);
            },
          ),
        ),
      ],
    );
  }
}

class DateAndTimePickerDemo extends StatefulWidget {
  static const String routeName = '/material/date-and-time-pickers';

  @override
  _DateAndTimePickerDemoState createState() =>
      new _DateAndTimePickerDemoState();
}

class _DateAndTimePickerDemoState extends State<DateAndTimePickerDemo> {
  String _days;
  String _temes;
  DateTime _fromDate = new DateTime.now();
  TimeOfDay _fromTime;
  DateTime _toDate = new DateTime.now();
  TimeOfDay _toTime;
  String time_result;

  @override
  void initState() {
    super.initState();
    int not_hour = _toDate.hour;
    int now_miniter = _toDate.minute;
    _toTime = new TimeOfDay(hour: not_hour, minute: now_miniter);
    _fromTime = new TimeOfDay(hour: not_hour, minute: now_miniter);
    time_result = _fromDate.year.toString() +
        "-" +
        _fromDate.month.toString() +
        "-" +
        _fromDate.day.toString() +
        " " +
        _fromTime.hour.toString() +
        ":" +
        _fromTime.minute.toString() +
        ":" +
        "00";
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        backgroundColor: Colors.blue,
        leading: InkWell(
          onTap: () {
            Navigator.of(context).pop();
          }, //故障申请表
          child: Icon(
            Icons.chevron_left,
            size: 29.0,
            color: Colors.white,
          ),
        ),
        centerTitle: true,
        title: Text(
          '时间选择',
          style: TextStyle(
              color: Colors.white, fontSize: 18.0, fontWeight: FontWeight.bold),
        ),
      ),
      body: new DropdownButtonHideUnderline(
        child: new SafeArea(
            top: false,
            bottom: false,
            child: Stack(
              alignment: Alignment.bottomCenter,
              children: <Widget>[
                new ListView(
                  padding: const EdgeInsets.all(16.0),
                  children: <Widget>[
                    new Container(
                      child: Shimmer.fromColors(
                        baseColor: Colors.blue,
                        highlightColor: Colors.white,
                        child: new Text(
                          time_result,
                          style: TextStyle(
                              color: Colors.blue,
                              fontSize: 16.0,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                    new _DateTimePicker(
                      labelText: '选择日期和具体时间',
                      selectedDate: _fromDate,
                      selectedTime: _fromTime,
                      selectDate: (DateTime date) {
                        setState(() {
                          _fromDate = date;
                          String yue;
                          String day;
                          if(_fromDate.month<10){
                            yue="0"+_fromDate.month.toString();
                          }else{
                            yue=_fromDate.month.toString();
                          }
                          if(_fromDate.day<10){
                            day="0"+_fromDate.day.toString();
                          }else{
                            day=_fromDate.day.toString();
                          }
                          DateTime now = DateTime.now();
                          // String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(now);
                          _days = date.year.toString() +
                              "-" +
                              date.month.toString() +
                              "-" +
                              date.day.toString();
                          time_result = _fromDate.year.toString() +
                              "-" +
                              yue +
                              "-" +
                              day+
                              " " +
                              _fromTime.hour.toString() +
                              ":" +
                              _fromTime.minute.toString() +
                              ":" +
                              "00";
                          print(_days);
                        });
                      },
                      selectTime: (TimeOfDay time) {
                        setState(() {
                          _fromTime = time;
                          _temes = time.hour.toString() +
                              ":" +
                              time.minute.toString() +
                              ":" +
                              "00";
                          String yue;
                          String day;
                          if(_fromDate.month<10){
                            yue="0"+_fromDate.month.toString();
                          }else{
                            yue=_fromDate.month.toString();
                          }
                          if(_fromDate.day<10){
                            day="0"+_fromDate.day.toString();
                          }else{
                            day=_fromDate.day.toString();
                          }
                          time_result = _fromDate.year.toString() +
                              "-" +
                              yue +
                              "-" +
                              day +
                              " " +
                              _fromTime.hour.toString() +
                              ":" +
                              _fromTime.minute.toString() +
                              ":" +
                              "00";
                          print(_temes);
                        });
                      },
                    ),
                  ],
                ),
                new InkWell(
                  onTap:(){
                    StaticList.ShijiStartTime=time_result;
                    Navigator.of(context).pop();
                  },
                  child: Container(
                    height: 35.0,
                    width: MediaQuery.of(context).size.width,
                    color: Color.fromRGBO(45, 46, 55, 22.0),
                    child: Center(
                      child: Text(
                        "保存",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                ),
              ],
            )),
      ),
    );
  }
}
